import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

// 初始化路由
const routes = [
    {
        path: '',
        component: () => import('../views/Home.vue'),
        children: [
            {
                path: '',
                name: '首页',
                component: () => import(/* webpackChunkName: "about" */ '../views/index/Dashboard.vue')
            },
            {
                path: 'UserManage',
                name: '用户管理',
                component: () => import('../views/sys/UserManage.vue')
            },
            {
                path: 'RoleManage',
                name: '角色管理',
                component: () => import('../views/sys/RoleManage.vue')
            },
            {
                path: 'MenuManage',
                name: '菜单管理',
                component: () => import('../views/sys/MenuManage.vue')
            },
            {
                path: 'UserSetting',
                name: '个人中心',
                component: () => import('../views/sys/UserSetting.vue')
            }
        ]
    },
    {
        path: '/login',
        name: '登录',
        component: () => import('../views/Login.vue')
    }
]

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    if (to.path === "/login") {
        next();
    } else {
        let token = localStorage.getItem("Authorization");
        if (token === null || token === '') {
            next("/login");
        } else {
            next();
        }
    }
})

export default router
